package cn.yh.register.hosp.controller;

import cn.yh.register.common.exception.RegisterException;
import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.MD5;
import cn.yh.register.hosp.service.HospitalSetService;
import cn.yh.register.model.hosp.HospitalSet;
import cn.yh.register.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;
@CrossOrigin //允许跨域访问
@Api(tags = "医院设置管理接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    HospitalSetService setService;

    //查询所有医院设置
    @ApiOperation("查询所有医院设置")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAll() {
        List<HospitalSet> list = setService.list();
        return Result.ok(list);
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @ApiImplicitParam(name = "id", value = "医院设置id", dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable Long id) {
        boolean flag = setService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //带条件的分页查询
    @ApiOperation("带条件的分页查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "页数", dataType = "Integer", paramType = "path"), @ApiImplicitParam(name = "limit", value = "条数", dataType = "Integer", paramType = "path")})

    @PostMapping("/findPageHospSet/{current}/{limit}")
    public Result<Page<HospitalSet>> findByPage(@PathVariable("current") Integer pageNo, @PathVariable("limit") Integer pageSize, @RequestBody(required = false) HospitalQueryVo queryVo) {
        //分页对象
        Page<HospitalSet> page = new Page<>(pageNo, pageSize);
        //条件对象
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();

        if (queryVo != null) {
            wrapper.like(!StringUtils.isEmpty(queryVo.getHosname()), HospitalSet::getHosname, queryVo.getHosname()).eq(!StringUtils.isEmpty(queryVo.getHoscode()), HospitalSet::getHoscode, queryVo.getHoscode());
        }

        Page<HospitalSet> hospitalSetPage = setService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }

    //添加医院设置
    @ApiOperation("添加医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "医院设置", dataType = "HospitalSet", paramType = "body")
    @PostMapping("/saveHospitalSet")
    public Result<?> save(@RequestBody HospitalSet hospitalSet) {
        //签名key  MD5（系统毫秒值+1000以内的随机数）
        String signKey = MD5.encrypt(System.currentTimeMillis() + "" + new Random(1000));
        hospitalSet.setSignKey(signKey);
        //状态 1:启用 0：禁用
        hospitalSet.setStatus(1);
        boolean flag = setService.save(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //根据id查询
    @ApiOperation("根据id查询")
    @ApiImplicitParam(name = "id", value = "医院设置id", dataType = "Long", paramType = "path")
    @GetMapping("/getHospSet/{id}")
    public Result<HospitalSet> findById(@PathVariable Long id) {

       /* try {
            int i=10/0;
        }catch (Exception e){
            throw new RegisterException("不允许除以零",2001);
        }*/

        HospitalSet hospitalSet = setService.getById(id);
        return Result.ok(hospitalSet);
    }

    //修改医院设置
    @ApiOperation("修改医院设置")
    @ApiImplicitParam(name = "hospitalSet", value = "医院设置", dataType = "HospitalSet", paramType = "body")
    @PutMapping("/updateHospitalSet")
    public Result<HospitalSet> update(@RequestBody HospitalSet hospitalSet) {
        boolean flag = setService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result<?> delete(@RequestBody List<Long> ids) {
        boolean flag = setService.removeByIds(ids);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //解锁锁定
    @ApiOperation("解锁锁定")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result<?> lock(@PathVariable Long id, @PathVariable Integer status) {
        HospitalSet hospitalSet = setService.getById(id);
        hospitalSet.setStatus(status);
        boolean flag = setService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();

    }
    //发送签名key
    @ApiOperation("发送签名key")
    @PutMapping("/sendKey/{id}")
    public Result<?> sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = setService.getById(id);
        //医院编号
        String hoscode = hospitalSet.getHoscode();
        //签名key
        String signKey = hospitalSet.getSignKey();
        // TODD 发送
        //.....
        //.....
        return Result.ok();
    }
}